package com.magicsoft.share.ui.article;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.utils.KeyboardUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class EdtContentActivity extends BaseActivity {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.edt)
    EditText edt;
    @BindView(R.id.tv_num)
    TextView tvNum;
    private String contentStr;

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_edt_content;
    }

    @Override
    public void initData() {

        contentStr = getIntent().getStringExtra("contentStr");
    }

    @Override
    public void initView() {
        ibtnLeft.setVisibility(View.GONE);
        tvLeft.setText("取消");
        tvRight.setText("完成");
        tvTitle.setText("编辑文字");

        edt.setText(contentStr);
        edt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int i = 5000 - editable.length();
                tvNum.setText("您还可以输入" + i + "字");
            }
        });

    }

    @Override
    public void requestData() {

    }


    @OnClick({R.id.tv_left, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                KeyboardUtils.closeKeybord(edt,this);
                Intent intent = new Intent();
                intent.putExtra("contentStr",edt.getText().toString());
                Log.e("SLL","执行"+edt.getText().toString());
                setResult(RESULT_OK,intent);
                finish();
                break;
            case R.id.tv_right:
                KeyboardUtils.closeKeybord(edt,this);
                if (edt.getText().length() == 0||edt.getText().toString().trim().equals("")) {
                    Log.e("SLL",edt.getText().toString());
                    break;
                }else {
                    Intent intent1 = new Intent();
                    intent1.putExtra("contentStr",edt.getText().toString());
                    Log.e("SLL","执行"+edt.getText().toString());
                    setResult(RESULT_OK,intent1);
                    finish();
                }
                break;
        }
    }
    //系统返回
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            KeyboardUtils.closeKeybord(edt,this);
            Intent intent = new Intent();
            intent.putExtra("contentStr",edt.getText().toString());
            Log.e("SLL","执行"+edt.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
